#include "cmdHandle/CWebUserInfo.h"
#include "CControlManage.h"
#include "CControlTool.h"
#include "CConfig.h"

#define WEB_USER_INFO_FILE "WebUserInfo.josn"

NETWORKSERVICE_BEGIN_NAMESPACE

// 单例运行
CWebUserInfo CWebUserInfo::m_Instance;

CWebUserInfo::CWebUserInfo()
:CNetBase(false)
,m_localUserInfo(string(SP_CONFIG_PATH) + WEB_USER_INFO_FILE)
{
	m_NetUserInfo = m_localUserInfo;
	ADD_CONTROL_CMD_TYPE("/login/setUser", CWebUserInfo::rSetUser, (T_UserInfo&)m_NetUserInfo, T_NullData(), true, false, false);
}

// 10分钟超时
void CWebUserInfo::timerOut()
{
    qDebug("timerOut()");
}

// 恢复默认
void CWebUserInfo::resetDefault()
{
	m_NetUserInfo = T_UserInfo();
	m_localUserInfo = T_UserInfo();
	m_localUserInfo.toFlie(string(SP_CONFIG_PATH) + WEB_USER_INFO_FILE);
}

// 接收到设置网页端用户名密码  由 /login/setUser 命令触发
bool CWebUserInfo::rSetUser(struct tRouteData<T_UserInfo&> *recv,struct tRouteData<T_NullData> *send, void *userData)
{
    qDebug("CWebUserInfo::rSetUser()");
    qDebug("%s",recv->msg.toJsonString().c_str());
	if(m_NetUserInfo.username.empty()){
        send->msg.msg = "账号为空";
        send->msg.code = NET_CODE_DATA_FAIL;
		return true;
	}
	if(m_NetUserInfo != m_localUserInfo){
        qDebug("save config:%s",m_NetUserInfo.toJsonString().c_str());
		m_localUserInfo = m_NetUserInfo;
		m_localUserInfo.toFlie(string(SP_CONFIG_PATH) + WEB_USER_INFO_FILE);
	}
	return true;
}


NETWORKSERVICE_END_NAMESPACE
